package com.example.venadaoa.datamanager;

import android.content.Context;

import com.example.venadaoa.tool.Constants;
import com.example.venadaoa.tool.PreferenceUitl;

import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by weng on 2017/7/11.
 */

public class UserInfo extends ServerResult implements Constants{





    public static UserInfo getUserInfor(Context c) {
        return  PreferenceUitl.getInstance(c).getBeanByGson(USER_INFO,UserInfo.class);
    }

    //是否有管理员权限
    public static boolean ishaveCode(Context c)
    {
        List<UserObjectBean.AuthoritiesBean> list =UserInfo.getUserInfor(c).getUserObject(). getAuthorities();
        for(int i=0;i<list.size();i++)
        {
            String role_code= list.get(i).getRole_code();
            if(role_code.equals("admin")||role_code.equals("hr"))
            {
                return true;
            }
        }
        return false;
    }

    /**
     * userObject : {"birthday":"","zip_code":"","isHead":"0","department":"技术部","userDetailId":"","office_fax":"","home_tel":"","passWord":"c4ca4238a0b923820dcc509a6f75849b","id":"1bf9ad3f-f1c0-4b80-ad45-e1ab6c85751f","home_address":"","QQ":"","name":"翁忆炯","gender":1,"userName":"wengyijiong","status":"0","lastModifyPdTime":1495163575000,"emailAddress":"","officeTel":"","companyName":"江苏沃购网络技术有限公司","leaders":{"userId":"760700a3-87e1-4a6c-8b6d-60a17730c7bd","userName":"孙文","departmentId":"1","departmentName":"总经办","companyId":"ed784569-4f59-43f6-be9f-61fb800df0c7","companyName":"江苏维纳达软件技术有限公司","isHead":"1","code":"nj","leaderId":null},"authorities":[{"id":"user","createTime":1407119650000,"createBy":null,"modifyTime":1407119653000,"modifyBy":null,"orderBy":null,"roleName":"用户","roleResources":null,"roleUsesrs":null,"description":null,"role_code":"user"}],"email":"","departmentId":"46021ec0-3106-4393-9ec4-edf53af876af","companyId":"20f84521-0c26-4d3a-bf2d-403edc01f241","mobileNumber":"18606231219"}
     * status : success
     */

    private UserObjectBean userObject;
    private String status;
    private boolean isLogin;

    private String msg;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }



    public boolean isLogin() {
        return isLogin;
    }

    public void setLogin(boolean login) {
        isLogin = login;
    }



    public UserObjectBean getUserObject() {
        return userObject;
    }

    public void setUserObject(UserObjectBean userObject) {
        this.userObject = userObject;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    //MD5加密
    public String MD5(String str) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");

        } catch (Exception e)

        {
            e.printStackTrace();
            return "";
        }
        char[] charArray = str.toCharArray();
        byte[] byteArray = new byte[charArray.length];
        for (int i = 0; i < charArray.length; i++) {
            byteArray[i] = (byte) charArray[i];
        }
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16) {
                hexValue.append("0");
            }
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();

    }


    public static class UserObjectBean {
        /**
         * birthday :
         * zip_code :
         * isHead : 0
         * department : 技术部
         * userDetailId :
         * office_fax :
         * home_tel :
         * passWord : c4ca4238a0b923820dcc509a6f75849b
         * id : 1bf9ad3f-f1c0-4b80-ad45-e1ab6c85751f
         * home_address :
         * QQ :
         * name : 翁忆炯
         * gender : 1
         * userName : wengyijiong
         * status : 0
         * lastModifyPdTime : 1495163575000
         * emailAddress :
         * officeTel :
         * companyName : 江苏沃购网络技术有限公司
         * leaders : {"userId":"760700a3-87e1-4a6c-8b6d-60a17730c7bd","userName":"孙文","departmentId":"1","departmentName":"总经办","companyId":"ed784569-4f59-43f6-be9f-61fb800df0c7","companyName":"江苏维纳达软件技术有限公司","isHead":"1","code":"nj","leaderId":null}
         * authorities : [{"id":"user","createTime":1407119650000,"createBy":null,"modifyTime":1407119653000,"modifyBy":null,"orderBy":null,"roleName":"用户","roleResources":null,"roleUsesrs":null,"description":null,"role_code":"user"}]
         * email :
         * departmentId : 46021ec0-3106-4393-9ec4-edf53af876af
         * companyId : 20f84521-0c26-4d3a-bf2d-403edc01f241
         * mobileNumber : 18606231219
         */

        private String birthday;
        private String zip_code;
        private String isHead;
        private String department;
        private String userDetailId;
        private String office_fax;
        private String home_tel;
        private String passWord;
        private String id;
        private String home_address;
        private String QQ;
        private String name;
        private int gender;
        private String userName;
        private String status;
        private long lastModifyPdTime;
        private String emailAddress;
        private String officeTel;
        private String companyName;
        private LeadersBean leaders;
        private String email;
        private String departmentId;
        private String companyId;
        private String mobileNumber;
        private List<AuthoritiesBean> authorities;

        public String getBirthday() {
            return birthday;
        }

        public void setBirthday(String birthday) {
            this.birthday = birthday;
        }

        public String getZip_code() {
            return zip_code;
        }

        public void setZip_code(String zip_code) {
            this.zip_code = zip_code;
        }

        public String getIsHead() {
            return isHead;
        }

        public void setIsHead(String isHead) {
            this.isHead = isHead;
        }

        public String getDepartment() {
            return department;
        }

        public void setDepartment(String department) {
            this.department = department;
        }

        public String getUserDetailId() {
            return userDetailId;
        }

        public void setUserDetailId(String userDetailId) {
            this.userDetailId = userDetailId;
        }

        public String getOffice_fax() {
            return office_fax;
        }

        public void setOffice_fax(String office_fax) {
            this.office_fax = office_fax;
        }

        public String getHome_tel() {
            return home_tel;
        }

        public void setHome_tel(String home_tel) {
            this.home_tel = home_tel;
        }

        public String getPassWord() {
            return passWord;
        }

        public void setPassWord(String passWord) {
            this.passWord = passWord;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getHome_address() {
            return home_address;
        }

        public void setHome_address(String home_address) {
            this.home_address = home_address;
        }

        public String getQQ() {
            return QQ;
        }

        public void setQQ(String QQ) {
            this.QQ = QQ;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getGender() {
            if(gender == 1)
            {
                return "男";
            }else
            {
                return "女";
            }

        }

        public void setGender(int gender) {
            this.gender = gender;
        }

        public String getUserName() {
            return userName;
        }

        public void setUserName(String userName) {
            this.userName = userName;
        }

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public String getLastModifyPdTime() {
            try {
                SimpleDateFormat mFormat= new SimpleDateFormat("yyyy-MM-dd");
                //继续转换得到秒数的long型

                return mFormat.format(new Date(lastModifyPdTime));

            } catch (Exception e) {
                // TODO: handle exception
            }

            return null;
        }

        public void setLastModifyPdTime(long lastModifyPdTime) {
            this.lastModifyPdTime = lastModifyPdTime;
        }

        public String getEmailAddress() {
            return emailAddress;
        }

        public void setEmailAddress(String emailAddress) {
            this.emailAddress = emailAddress;
        }

        public String getOfficeTel() {
            return officeTel;
        }

        public void setOfficeTel(String officeTel) {
            this.officeTel = officeTel;
        }

        public String getCompanyName() {
            return companyName;
        }

        public void setCompanyName(String companyName) {
            this.companyName = companyName;
        }

        public LeadersBean getLeaders() {
            return leaders;
        }

        public void setLeaders(LeadersBean leaders) {
            this.leaders = leaders;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }

        public String getDepartmentId() {
            return departmentId;
        }

        public void setDepartmentId(String departmentId) {
            this.departmentId = departmentId;
        }

        public String getCompanyId() {
            return companyId;
        }

        public void setCompanyId(String companyId) {
            this.companyId = companyId;
        }

        public String getMobileNumber() {
            return mobileNumber;
        }

        public void setMobileNumber(String mobileNumber) {
            this.mobileNumber = mobileNumber;
        }

        public List<AuthoritiesBean> getAuthorities() {
            return authorities;
        }

        public void setAuthorities(List<AuthoritiesBean> authorities) {
            this.authorities = authorities;
        }

        public static class LeadersBean {
            /**
             * userId : 760700a3-87e1-4a6c-8b6d-60a17730c7bd
             * userName : 孙文
             * departmentId : 1
             * departmentName : 总经办
             * companyId : ed784569-4f59-43f6-be9f-61fb800df0c7
             * companyName : 江苏维纳达软件技术有限公司
             * isHead : 1
             * code : nj
             * leaderId : null
             */

            private String userId;
            private String userName;
            private String departmentId;
            private String departmentName;
            private String companyId;
            private String companyName;
            private String isHead;
            private String code;
            private Object leaderId;

            public String getUserId() {
                return userId;
            }

            public void setUserId(String userId) {
                this.userId = userId;
            }

            public String getUserName() {
                return userName;
            }

            public void setUserName(String userName) {
                this.userName = userName;
            }

            public String getDepartmentId() {
                return departmentId;
            }

            public void setDepartmentId(String departmentId) {
                this.departmentId = departmentId;
            }

            public String getDepartmentName() {
                return departmentName;
            }

            public void setDepartmentName(String departmentName) {
                this.departmentName = departmentName;
            }

            public String getCompanyId() {
                return companyId;
            }

            public void setCompanyId(String companyId) {
                this.companyId = companyId;
            }

            public String getCompanyName() {
                return companyName;
            }

            public void setCompanyName(String companyName) {
                this.companyName = companyName;
            }

            public String getIsHead() {
                return isHead;
            }

            public void setIsHead(String isHead) {
                this.isHead = isHead;
            }

            public String getCode() {
                return code;
            }

            public void setCode(String code) {
                this.code = code;
            }

            public Object getLeaderId() {
                return leaderId;
            }

            public void setLeaderId(Object leaderId) {
                this.leaderId = leaderId;
            }
        }

        public static class AuthoritiesBean {
            /**
             * id : user
             * createTime : 1407119650000
             * createBy : null
             * modifyTime : 1407119653000
             * modifyBy : null
             * orderBy : null
             * roleName : 用户
             * roleResources : null
             * roleUsesrs : null
             * description : null
             * role_code : user
             */

            private String id;
            private long createTime;
            private Object createBy;
            private long modifyTime;
            private Object modifyBy;
            private Object orderBy;
            private String roleName;
            private Object roleResources;
            private Object roleUsesrs;
            private Object description;
            private String role_code;

            public String getId() {
                return id;
            }

            public void setId(String id) {
                this.id = id;
            }

            public long getCreateTime() {
                return createTime;
            }

            public void setCreateTime(long createTime) {
                this.createTime = createTime;
            }

            public Object getCreateBy() {
                return createBy;
            }

            public void setCreateBy(Object createBy) {
                this.createBy = createBy;
            }

            public long getModifyTime() {
                return modifyTime;
            }

            public void setModifyTime(long modifyTime) {
                this.modifyTime = modifyTime;
            }

            public Object getModifyBy() {
                return modifyBy;
            }

            public void setModifyBy(Object modifyBy) {
                this.modifyBy = modifyBy;
            }

            public Object getOrderBy() {
                return orderBy;
            }

            public void setOrderBy(Object orderBy) {
                this.orderBy = orderBy;
            }

            public String getRoleName() {
                return roleName;
            }

            public void setRoleName(String roleName) {
                this.roleName = roleName;
            }

            public Object getRoleResources() {
                return roleResources;
            }

            public void setRoleResources(Object roleResources) {
                this.roleResources = roleResources;
            }

            public Object getRoleUsesrs() {
                return roleUsesrs;
            }

            public void setRoleUsesrs(Object roleUsesrs) {
                this.roleUsesrs = roleUsesrs;
            }

            public Object getDescription() {
                return description;
            }

            public void setDescription(Object description) {
                this.description = description;
            }

            public String getRole_code() {
                return role_code;
            }

            public void setRole_code(String role_code) {
                this.role_code = role_code;
            }
        }
    }
}
